import Vue from "vue"
import Router from "vue-router"
import NProgress from "nprogress"
import "nprogress/nprogress.css"
import store from "vuex"
import initTheme from "@/components/common/ThemeSetting/initTheme"

import { getParameterByName } from "@/utils/utils"


Vue.use(Router)

const routes = [
  {
    path: "/",
    name: "home",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: () => import("@/views/home"),
  },
  {
    path: "/board",
    name: "board",
    component: () => import("@/views/board/BoardList"),
  },
  {
    path: "/board/boardEdit",
    name: "boardEdit",
    component: () => import("@/views/board/BoardEdit"),
  },
  {
    path: "/board/boardPreview",
    name: "boardPreview",
    component: () => import("@/views/board/BoardPreview"),
  }
]
const router = new Router({
  // mode: "history",
  routes,
})

const blackList = ""

router.beforeEach(async (to, from, next) => {
  NProgress.start()

  // 加载皮肤
  const styleId = getParameterByName("styleId" ,to.fullPath)
  const color = getParameterByName("color" ,to.fullPath.replace("#", ""))
  initTheme({styleId, color: color ? `#${color}` : null})
  const token = ""
  if (token) {
    // 用户信息不存在
    if (!store.getters.userInfo) {
      // await store.dispatch('GetUser'); // 获取用户信息
      // router.addRoutes(store.getters.addRoutes);
    } else {
      next()
    }
  } else if (blackList.includes(to.path)) {
    // window.location.href = store.getters.api.IPORTAL_LOCAL_API;
    NProgress.done()
  } else {
    // 不是黑名单，直接进入
    next()
  }
})
router.afterEach(() => {
  NProgress.done()
  router.afterEach((to, from, next) => {

    document.body.scrollTop = 0
    document.documentElement.scrollTop = 0
  })
})
export default router
